package cn.zooz.admin.config.ds;

import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.enums.IdType;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * Created by Bradish7Y on 2017/9/20.
 */
@Configuration
@EnableTransactionManagement
@ImportResource({"classpath:spring/spring-transaction.xml"})
@MapperScan(value = "cn.zooz.dao", sqlSessionFactoryRef = "sqlSessionFactory")
public class DataSourceConfig {
    @Value("${spring.datasource.type}")
    private Class<? extends DataSource> dataSourceType;

    /**
     * 配置druid数据源，需配置，否则不生效
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().type(dataSourceType).build();
    }


    @Bean
    public GlobalConfiguration globalConfigurationRW() {
        GlobalConfiguration g = new GlobalConfiguration();
        g.setIdType(IdType.AUTO.getKey());

        return g;
    }

    @Bean
    public Properties properties() {
        Properties prop = new Properties();
        prop.setProperty("cacheModelsEnabled", "true");
        prop.setProperty("enhancementEnabled", "true");
        prop.setProperty("lazyLoadingEnabled", "false");
        prop.setProperty("errorTracingEnabled", "true");
        prop.setProperty("maxRequests", "32");
        prop.setProperty("maxSessions", "10");
        prop.setProperty("maxTransactions", "5");
        prop.setProperty("useStatementNamespaces", "true");

        return prop;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        //实体类所在的包
        sessionFactory.setTypeAliasesPackage("cn.zooz.entity");

        sessionFactory.setConfigurationProperties(properties());

        sessionFactory.setGlobalConfig(globalConfigurationRW());

        //mapper*.xml的位置，特殊需求还是要配置在xml里
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
                getResources("classpath:mapper/*.xml"));

        //分页
        PaginationInterceptor pageInterceptor = new PaginationInterceptor();
        Properties pageProp = new Properties();
        //pageProp.setProperty("dialectType", "mysql");
        pageInterceptor.setProperties(pageProp);

        sessionFactory.setPlugins(new Interceptor[]{pageInterceptor});//分页
        return sessionFactory.getObject();
    }

}
